<?php
if (!defined("OK")) { header("location: ?"); exit; }
if(isset($url['k']) && isnum($url['k']) && $url['k'] > 0) { $kid = (int)$url['k']; } else { $kid = 0; }
//if(isset($url['m']) && isnum($url['m']) && $url['m'] > 0) { $mid = (int)$url['m']; } else { $mid = 0; }
if (isset($url['p']) && isnum($url['p']) && $url['p'] > 0) { $p = (int)$url['p']; } else { $p = 0; }

if ($conf['Palaikymas'] == 1) { klaida("Remontas",$conf['Maintenance'],"Svetainė uždaryta"); }
$limit = $conf['News_limit'];
$viso = kiek("naujienos");

$text = '';
// Jei paspaudziamas linkas "Komenatarai"
if ($kid != 0) {
	$sql = mysql_fetch_assoc(mysql_query1("SELECT SQL_CACHE * FROM ".LENTELES_PRIESAGA."naujienos WHERE id=".escape($kid)));
	if (mysql_affected_rows() > 0) {
		$title = $sql['pavadinimas'];
		$text = "<div class='sarasas'>".$sql['naujiena']."</div>";
		if (!empty($sql['daugiau'])) { $text .= ''.$sql['daugiau']; }
		$text .= "<hr /><blockquote><em>patalpinta ".$sql['data']." autorius: <b>".$sql['autorius']."</b></em></blockquote>";
		//Rodom komentarus
		$data = explode(' ',$sql['data']);
		$data = explode('-',$data[0]);
		lentele($title,$text,false,array(menesis($data[1]),$data[2]));
		if ($sql['kom'] == 'taip') {
			include("priedai/komentarai.php");
			komentarai($kid,true);
		}
		unset($text,$title);
	}
	else { klaida("Klaida","Tokios naujienos nėra arba ji buvo ištrinta"); redirect("?id,".(int)$_GET['id'],"meta"); }
}

// Jei niekas nepaspaudziama, o atidaromas pirminis puslapis
elseif ($kid == 0) {

	$sql = mysql_query1("SELECT SQL_CACHE *,(SELECT SQL_CACHE COUNT(*) FROM `".LENTELES_PRIESAGA."kom` WHERE `pid`=50 AND `".LENTELES_PRIESAGA."kom`.`kid` = `".LENTELES_PRIESAGA."naujienos`.`id`) AS `viso` FROM `".LENTELES_PRIESAGA."naujienos` ORDER BY `data` DESC LIMIT $p,$limit");
	while ($row = mysql_fetch_assoc($sql)) {
		$extra = "<div style='float: right'>".(($row['kom'] == 'taip')?"<a href='?id,50;k," . $row['id'] ."'>Skaityti toliau...• Komentarai (" . $row['viso'] .")</a> ":"<a href='?id,50;k," . $row['id'] ."'>Skaityti toliau...</a>")."</div>";
		$data = explode(' ',$row['data']);
		$data = explode('-',$data[0]);
		$kategorijos_pav = mysql_fetch_assoc(mysql_query1("SELECT `pav` FROM `".LENTELES_PRIESAGA."naujienos_kat` WHERE `id` = ".escape($row['kategorija'])));
		$kategorijos_pav = "<table><tr valign='top'><td style='padding-right:10px'><div class='avataras' style='margin-right:25px'><img src='images/naujienu_kat/".input($kategorijos_pav['pav'])."' alt='Kategorija' /></div></td><td>";
		lentele($row['pavadinimas'],$kategorijos_pav.$row['naujiena']."</td></tr></table>".$extra,false,array(menesis($data[1]),$data[2]));
	}

	if ($viso >= $limit) { lentele("Puslapiai",puslapiai($p,$limit,$viso,10),false,array('Viso',$viso)); }
	unset($sql,$row,$extra);
}
?>

